var setting;
var lastValue = "", nodeList = [], fontCss = {};

selectedAuthority = {
	initZTree : function(rootNode){
		$.fn.zTree.init($("#"+rootNode), setting);
	},
	settingInit : function(posturl) {
		setting = {
			check : {
				/** 复选框* */
				enable : true,
				chkboxType : {// 复选框
					"Y" : "",// 复选框
					"N" : ""// 复选框
				}
			},
			view : {
				dblClickExpand: false,
			},
			data : {
				simpleData : {// 数据是否采用简单 Array 格式，默认false
					enable : true,
					idKey : "id",
					pIdKey : "pid",
					rootPId : 0
				}
			},
			async : {
				enable : true,
				url : posturl,
				type : "post",
				contentType : "application/json"
			},
			callback : {
				onAsyncSuccess : selectedAuthority.zTreeOnAsyncSuccess,
				onAsyncError : selectedAuthority.zTreeOnAsyncError,
				beforeCheck: selectedAuthority.beforeCheck,
				onClick : selectedAuthority.onCheckBox
			}
		};
	},
	zTreeOnAsyncSuccess : function(event, treeId, treeNode){
    	$("body").hideLoading();
    	selectedAuthority.initCheck($("#checkIds").val());
    },
    zTreeOnAsyncError : function(event, treeId, treeNode){
    	
    },
    beforeCheck : function(treeId, treeNode) {
    	var v = $("#checkName").val();
		var ids = $("#checkIds").val();
    	selectedAuthority.isBeforeCheckValue(treeNode);
		return (treeNode.check !== false);
	},
    onCheckBox : function(e, treeId, treeNode) {
    	selectedAuthority.isCheckValue(treeNode);
	},
	isCheckValue : function(treeNode){
		//选标题选值
		if(treeNode.pId!=null&&!treeNode.isParent){
			var zTree = $.fn.zTree.getZTreeObj("treeDemo");
			if(treeNode.checked!=undefined){
				zTree.checkNode(treeNode, !treeNode.checked, null, true);
			}
			var nodes = zTree.getCheckedNodes(true), v = "",ids="";
			for (var i = 0, l = nodes.length; i < l; i++) {
				v += nodes[i].name + "#";
				ids += nodes[i].id+"#";
			}
			if (v.length > 0)
				v = v.substring(0, v.length - 1);
			if (ids.length > 0)
				ids = ids.substring(0, ids.length - 1);
			$("#checkName").attr("value", v);
			$("#checkIds").attr("value",ids);
		}
	},
	isBeforeCheckValue : function(treeNode){
		//选复选框
		var v = $("#checkName").val();
		var ids = $("#checkIds").val();
		if(!treeNode.checked){
			if(ids!=''){
				v = v +"#"+treeNode.name
				ids = ids +"#"+treeNode.id
			}else{
				v = treeNode.name
				ids = treeNode.id
			}
		}else{
			v = functionUtil.replaceAllValue(functionUtil.replaceAllValue(functionUtil.replaceAllValue(v,treeNode.name+"#",""),"#"+treeNode.name,""),treeNode.name,"");
			ids = functionUtil.replaceAllValue(functionUtil.replaceAllValue(functionUtil.replaceAllValue(ids,treeNode.id+"#",""),"#"+treeNode.id,""),treeNode.id,"");
		}
		$("#checkName").attr("value", v);
		$("#checkIds").attr("value",ids);
	},
	initCheck : function(ids){
		//初始化选中。
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = zTree.getNodes()[0].children;//获取子点节,,zTree.getNodes();
		if(ids!=''){
			var arr = ids.split('#',-1);
			$.each(nodes, function(i, item) {
				$.each(arr, function(i, id) {
					if(item.id==id){
						zTree.checkNode(item, true, true);
					}
				});
			});
		}
	},
	returnIsCheck : function(){
		var array={};
		var ids = $("#checkIds").val();
		var v = $("#checkName").val();
		if(ids!=''){
			var html ='';
			var arrids = ids.split('#',-1);
			var arrname = v.split('#',-1);
			$.each(arrids, function(i, id) {
				if(i%2==0){
					html +='<button type="button" id="authority_'+id+'" class="btn btn-xs btn-info" style="margin-bottom:5px;margin-right:15px;">【'+arrname[i]+'】&nbsp;</button><span class="btn-danger circle_del" id="authority_span_'+id+'" title="移除" onclick="removeAu('+id+',\''+arrname[i]+'\')">x</span>';
				}else{
					html +='<button type="button" id="authority_'+id+'" class="btn btn-xs btn-success" style="margin-bottom:5px;margin-right:15px;">【'+arrname[i]+'】&nbsp;</button><span class="btn-danger circle_del" id="authority_span_'+id+'" title="移除" onclick="removeAu('+id+',\''+arrname[i]+'\')">x</span>';
				}
			});
			array.ids=ids;
			array.showname = html;
			array.checkName = v;
			return array;
		}
		return "";
	}
}
